package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;

/**
 * 作者:dugaofeng on 2024/12/30 10:46
 */
public class S5ModeSettingItemContentView extends LinearLayout implements ModeSettingItemView.OnModeSettingItemViewListner {

    private ModeSettingBean.ModeSettingStepBean settingStepBean;

    private ModeSettingItemView rotationItemView;   //旋转球
    private ModeSettingItemView frequencyItemView;   //发球频率
    private ModeSettingItemView speedItemView;   //发球速度
    private ModeSettingItemView horizontalIItemView;   //落点角度（水平方向）

    private S5ModeSettingItemContentView.OnModeSettingItemListner onModeSettingItemListner;

    public S5ModeSettingItemContentView(@NonNull Context context) {
        this(context, null);
    }

    public S5ModeSettingItemContentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public S5ModeSettingItemContentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initListner();
    }

    public void setOnModeSettingItemListner(OnModeSettingItemListner onModeSettingItemListner) {
        this.onModeSettingItemListner = onModeSettingItemListner;
    }

    private void initView() {
        inflate(getContext(), R.layout.view_mode_content_s5, this);

        frequencyItemView = findViewById(R.id.item_frequency);
        speedItemView = findViewById(R.id.item_speed);
        rotationItemView = findViewById(R.id.item_rotation);
        horizontalIItemView = findViewById(R.id.item_horizontal);
    }

    private void initListner() {
        frequencyItemView.setOnModeSettingItemViewListner(this);
        speedItemView.setOnModeSettingItemViewListner(this);
        rotationItemView.setOnModeSettingItemViewListner(this);
        horizontalIItemView.setOnModeSettingItemViewListner(this);
    }

    public void setSettingStepBean(ModeSettingBean.ModeSettingStepBean settingStepBean) {
        this.settingStepBean = settingStepBean;

        frequencyItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagFrequency);
        frequencyItemView.setSettingItemBean(settingStepBean.getFrequencyItemBean());   //发球频率

        speedItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagSpeed);
        speedItemView.setSettingItemBean(settingStepBean.getSpeedItemBean());   //发球速度

        //旋转球  0.不旋  1上旋  2下旋
        if (settingStepBean.getRotationType() == 1) {
            rotationItemView.setMutiItem(true);
            rotationItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagUpRotation);
            rotationItemView.setSettingItemBean(settingStepBean.getUpRotationItemBean());
        } else if (settingStepBean.getRotationType() == 2){
            rotationItemView.setMutiItem(true);
            rotationItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagDownRotation);
            rotationItemView.setSettingItemBean(settingStepBean.getDownRotationItemBean());
        } else if (settingStepBean.getRotationType() == 0) {
            rotationItemView.setMutiItem(true);
            rotationItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagNoRotation);
            rotationItemView.setSettingItemBean(settingStepBean.getNoRotationItemBean());
        }

        horizontalIItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagHorizontal);
        horizontalIItemView.setSettingItemBean(settingStepBean.getHorizontalItemBean());   //落点角度（水平方向）
    }

    @Override
    public void onModeSettingItemViewValueClick(ModeSettingItemView itemView) {
        if (onModeSettingItemListner != null) {
            onModeSettingItemListner.onModeSettingItemValueClick(settingStepBean, itemView.getSettingItemBean(), itemView.getItemTag());
        }
    }

    public interface OnModeSettingItemListner {
        void onModeSettingItemValueClick(ModeSettingBean.ModeSettingStepBean stepBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag);
    }
}
